require('./page.css');
let cp = require('cp');
const Base64 = require('js-base64').Base64;
cp.initPage();
// let page_main = requirKre('./_order-details.html');
function goPay(payment) {
  let newtab = window.open('about:blank');
  switch (payment) {
    case 1:// 支付宝
      let href = 'https://www.ccian.com/gateway/alipay.trade.page.pay.jsp?WIDout_trade_no=' +
      vm.orderno +
      '&WIDtotal_amount=' + vm.act_baseinfo.price / 100 +
      '&WIDsubject=' + vm.act_baseinfo.shoptempname +
      ' - 店铺模板&WIDbody=烽报活动号店铺模板支持微信小程序、支付宝小程序';
      newtab.location = href;
      break;
    case 2:// 微信

      break;
  }
  cp.layerConfirm({
    msg: '正在支付...',
    btn: ['支付成功', '支付遇到问题？'],
    yes: () => {
      window.location.href = cp.PAGE_ROOT + 'index/orderdetail/page.html#/' + Base64.encode(vm.orderno);
    },
    cancel: () => {
      window.location.href = cp.PAGE_ROOT + 'index/orderdetail/page.html#/' + Base64.encode(vm.orderno);
    },
  });
}
let vm = avalon.define({
  $id: 'mc_tickets',
  act_baseinfo: {}, // 活动基本信息
  addrinfo: {}, // 活动地址
  orderno: 0, // 用于储存付款订单号
  payment: 1, // 支付方式
  invoice: 2, // 收票人类型
  checked: false, // 发票显示
  showContact: true,
  invoice_pass: false,
  contact_info: {
    name: '',
    mobile: '',
    company: '',
    title: '',
  },
  invoice_address: {
    province: '',
    city: '',
    district: '',
    street: '',
  },
  goodsorderbill: {
    orderno: 0,
    types: 2, // 发票类型：1.个人 2.公司
    header: '',
    taxpayerno: '',
    email: '',
    mobile: '',
    name: '',
    receive: 1, // 收票方式 1.现场 2.快递
    postcode: '',
  },
  validate_contact: {
    // 联系人验证
    onValidateAll: reasons => {
      if (reasons.length) {
        cp.layerMsg({
          msg: reasons[0].getMessage(),
        });
      } else {
        if (vm.orderno) {
          cp.ajaxUrl({
            // 提交联系人
            controller: 'shopmgr',
            cmd: 'com.rel.porg.pb.shop_contact_info_req',
            data: {
              req: 0,
              orderno: vm.orderno,
              contact: vm.contact_info,
            },
            success: res => {
              vm.contact_info.enrollid = res.contactId;
              vm.showContact = false;
            },
          });
        } else {
          cp.layerMsg({msg: '订单信息错误，请重新确认订单'});
        }
      }
    },
  },
  validate_invoice: {
    onValidateAll: reasons => {
      if (reasons.length) {
        vm.invoice_pass = false;
        cp.layerMsg({
          msg: reasons[0].getMessage(),
        });
      } else {
        let data;
        if (vm.goodsorderbill.receive === 1) {
          data = {
            req: 0,
            orderno: vm.orderno,
            goodsorderbill: vm.goodsorderbill,
          };
        } else {
          data = {
            req: 0,
            orderno: vm.orderno,
            address: vm.invoice_address,
            goodsorderbill: vm.goodsorderbill,
          };
        }
        cp.ajaxUrl({
          controller: 'goods',
          cmd: 'com.rel.porg.pb.goods_oder_bill_req',
          data: data,
          async: false,
          success: res => {
            vm.invoice_pass = true;
          },
          fail: res => {
            vm.invoice_pass = false;
            cp.layerMsg({
              msg: '发票信息保存失败，请重试',
            });
          },
        });
      }
    },
  },
  bindGopay: (e, payment) => {
    // 前往付款
    if (vm.orderno && vm.contact_info.enrollid) {
      if (vm.checked) {
        vm.validate_invoice.onManual();
        setTimeout(() => {
          if (vm.invoice_pass) {
            goPay(e, payment);
          }
        });
      } else {
        goPay(e, payment);
      }
    } else {
      cp.layerMsg({msg: '订单信息错误，请重新提交订单！'});
    }
  },
});

require('router/mmRouter.js');
avalon.router.add('/:id', function(id) {
  let goodsid = Number(id.substring(5, id.length));
  cp.ajaxUrl({
    // 获取活动基本信息
    controller: 'shopskt',
    cmd: 'com.rel.porg.pb.shop_sel_order_temp_price_req',
    async: false,
    data: {
      goodsid: goodsid,
    },
    success: res => {
      vm.act_baseinfo = res.info;
      vm.orderno = res.info.orderno;
      cp.ajaxUrl({
        // 获取默认联系人
        controller: 'goods',
        cmd: 'com.rel.porg.pb.find_default_enroller_req',
        data: {},
        success: res => {
          if (!res.actenroller.title) {
            res.actenroller.title = '';
          }
          vm.contact_info = {
            name: res.actenroller.name,
            mobile: res.actenroller.mobile,
            company: res.actenroller.company,
            title: res.actenroller.title,
          };
        },
      });
    },
    fail: () => {
      cp.layerMsg('店铺模板不存在');
    },
  });
});

// 启动路由监听
avalon.history.start({
  root: '/act/index/index/page.html',
  hashPrefix: '',
});
